//
//  MainmenuLayer.cpp
//  DiceWar
//
//  Created by Xiao  Feng on 17/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "MainmenuLayer.h"
#include "AppDelegate.h"

bool MainmenuLayer::init()
{
    if (!CCLayer::init()) return false;
    if (!initBackground()) return false;
    if (!initButtons()) return false;
    
    return true;
}

bool MainmenuLayer::initBackground()
{
    cocos2d::CCSprite* pBackground = cocos2d::CCSprite::spriteWithFile("MainmenuBackground.png");
    CCAssert(pBackground, "Background sprite can't be create!");
    
    const cocos2d::CCSize kWinSize = cocos2d::CCDirector::sharedDirector()->getWinSize();
    pBackground->setPosition(ccp(kWinSize.width/2, kWinSize.height/2));
    pBackground->setScale(0.5f);
    
    addChild(pBackground, 1);
    return true;
}

bool MainmenuLayer::initButtons()
{
    cocos2d::CCMenu* pMenu = cocos2d::CCMenu::node();
    CCAssert(pMenu, "Menu can't be created!");
    addChild(pMenu, 1);
    
    const cocos2d::CCSize kWinSize = cocos2d::CCDirector::sharedDirector()->getWinSize();
    
    using namespace cocos2d;
    cocos2d::CCMenuItemImage* pStartButton = cocos2d::CCMenuItemImage::itemWithNormalImage("greenButton.png", "blueButton1.png", this, menu_selector(MainmenuLayer::onStartButtonDown));
    CCAssert(pStartButton, "Button can't be created");
    
    pStartButton->setPosition(ccp(kWinSize.width/3, kWinSize.height/3));
    pMenu->addChild(pStartButton);
    
    return true;
}

void MainmenuLayer::onStartButtonDown(cocos2d::CCObject* pObject)
{
    SceneManager::sharedSceneManager().switchToScene(SceneManager::kGameScene);
    CCLOG("On Start Button Down");
}